﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Skynet
{
    public partial class NodeEditorM : Form
    {
        public IconNode b;
        public frmSkynet pf;
        public NodeEditorM()
        {
            InitializeComponent();
        }

        private void NodeEditor_Load(object sender, EventArgs e)
        {
            b = (IconNode ) (this.Tag) ;
            
            this.cmbDomain.Items.Clear();
            pf = (frmSkynet) Application.OpenForms[0];
            
            foreach (Domain domain in pf.domains.domList)
            {
                var flag = false;
                foreach (Value value in pf.domains.valList)
                {
                    if (value.domainRow==domain.Row)
                    {
                        flag = true;
                    }
                }
                if (flag)
                {
                    cmbDomain.Items.Add(domain.Row.Cells[0].Value.ToString());
                }
                
            }
            this.cmbValue.Items.Clear();
           
           
            if (b.domain != null)
            {
                cmbDomain.SelectedItem = b.domain;
                foreach (Value value in pf.domains.valList)
                {
                    if (value.domainRow == pf.domains.domList[0].Row)
                    {
                        cmbValue.Items.Add(value.Row.Cells[0].Value.ToString());
                    }
                    
                }
                if (b.value != null) cmbValue.SelectedItem = b.value;
                
            }
     
            
        }

        

        private void cmbDomain_SelectedIndexChanged(object sender, EventArgs e)
        {
            var a = cmbDomain.SelectedItem.ToString();
            var d = (from syn in pf.domains.valList
                     where syn.domainRow.Cells[0].Value.ToString() == a
                     select syn.Row.Cells[0].Value.ToString()).ToList();
            cmbValue.Items.Clear();
            foreach (string syn in d)
            {
                cmbValue.Items.Add(syn);
            }
            if (d.Count > 0)
            {
                cmbValue.SelectedItem = d[0];
                cmbValue.Refresh();
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (cmbDomain.SelectedItem != null)
            {
                var d = (from domain in pf.domains.domList
                         where (domain.Row.Cells[0].Value.ToString() == cmbDomain.SelectedItem.ToString())
                         select domain).FirstOrDefault();
                b.domain = d.Row.Cells[0].Value.ToString();
                b.Label = d.Row.Cells[0].Value.ToString();
                
            }
            if (cmbValue.SelectedItem != null)
            {
                var d = (from value in pf.domains.valList
                         where (value.Row.Cells[0].Value.ToString() == cmbValue.SelectedItem.ToString())
                         select value).FirstOrDefault();
                b.value = d.Row.Cells[0].Value.ToString();
            }
            else
            {
                if (cmbValue.Text.Trim()!= "")
                {
                    if (cmbDomain.SelectedItem != null)
                    {
                        // завести новое
                        var val = new Value();
                        var row = new DataGridViewRow();
                        row.Cells.Add(new DataGridViewTextBoxCell());
                        row.Cells[0].Value = cmbValue.Text.Trim();
                        val.Row = row;
                        val.domainRow =
                            (from domain in pf.domains.domList
                             where (domain.Row.Cells[0].Value.ToString() == cmbDomain.SelectedItem.ToString())
                             select domain.Row).FirstOrDefault();
                            pf.domains.valList.Add(val);
                      
                        // заполнить ноде

                        b.value = cmbValue.Text.Trim();
                    }
                   
                }

                                
                

            }
            
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

     

        

        
    }
}
